package com.powerview.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.GET;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.HTablePool;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.powerview.model.Read;

@Controller
@RequestMapping("/power")
public class HbaseController {  

	public static Configuration configuration;  

	static {  
		configuration = HBaseConfiguration.create();  
		configuration.set("hbase.zookeeper.property.clientPort", "2181");  //端口
		configuration.set("hbase.zookeeper.quorum", "172.17.0.2");    //zookeeper地址
		configuration.set("hbase.master", "172.17.0.2:60000");  	  //hbase地址
	}  

	/**
	 * 电网稳定曲线
	 * @return
	 */
	@RequestMapping(value = "/selectDWwwdqx", method = RequestMethod.POST)
	@ResponseBody
	public List<Read> selectDWwwdqx(){
		List<Read> list = new ArrayList<Read>();
		list = QueryHbaseTable("curve");
		System.out.println("电网稳定曲线");
		return list;
	}

	/**
	 * 六大区电力负荷
	 * @return
	 */
	@RequestMapping(value = "/selectLddqdlfh", method = RequestMethod.POST)
	@ResponseBody
	public List<Read> selectLddqdlfh(){
		List<Read> list = new ArrayList<Read>();
		list = QueryHbaseTable("load");
		System.out.println("六大区电力负荷");
		return list;
	}

	/**
	 * 电力频率全国分布情况
	 * @return
	 */
	@RequestMapping(value = "/selectDlplqgfbqk", method = RequestMethod.POST)
	@ResponseBody
	public List<Read> selectDlplqgfbqk(){
		List<Read> list = new ArrayList<Read>();
		list = QueryHbaseTable("distribution");
		System.out.println("电力频率全国分布情况");
		return list;
	}

	/**
	 * 仪表盘
	 * @return
	 */
	@RequestMapping(value = "/selectYbp", method = RequestMethod.POST)
	@ResponseBody
	public List<Read> selectYbp(){
		List<Read> list = new ArrayList<Read>();
		list = QueryHbaseTable("security");
		System.out.println("仪表盘");
		return list;
	}

	/**
	 * 中国地图
	 * @return
	 */
	@RequestMapping(value = "/selectZgdt", method = RequestMethod.POST)
	@ResponseBody
	public List<Read> selectZgdt(){
		List<Read> list = new ArrayList<Read>();
		list = QueryHbaseTable("power");
		System.out.println("中国地图");
		return list;
	}

	/**
	 * 关键节点稳定指标
	 * @return
	 */
	@RequestMapping(value = "/selectloadGjjdwdzb", method = RequestMethod.POST)
	@ResponseBody
	public List<Read> selectloadGjjdwdzb(){
		List<Read> list = new ArrayList<Read>();
		list = QueryHbaseTable("sindex");
		System.out.println("关键节点稳定指标");
		return list;
	}

	/**
	 * 策略表
	 * @return
	 */
	@RequestMapping(value = "/selectClb", method = RequestMethod.POST)
	@ResponseBody
	public List<Read> selectClb(){
		List<Read> list = new ArrayList<Read>();
		list = QueryHbaseTable("strategy");
		System.out.println("策略表");
		return list;
	}

	/**
	 * 敲淮南等值参数
	 * @return
	 */
	@RequestMapping(value = "/selectloadQhndzcs", method = RequestMethod.POST)
	@ResponseBody
	public List<Read> selectloadQhndzcs(){
		List<Read> list = new ArrayList<Read>();
		list = QueryHbaseTable("equivalent");
		System.out.println("敲淮南等值参数");
		return list;
	}

	/** 
	 * 查询所有数据 
	 * @param tableName 
	 */  
	@SuppressWarnings("deprecation")
	public static void QueryAll(String tableName) {  
		HTablePool pool = new HTablePool(configuration, 1000);    
		HTableInterface table = (HTableInterface) pool.getTable(tableName);
		try {  
			ResultScanner rs = table.getScanner(new Scan());  
			for (Result r : rs) {  
				System.out.println("获得到rowkey:" + new String(r.getRow()));  
				for (KeyValue keyValue : r.raw()) {  
					System.out.println("列：" + new String(keyValue.getFamily())+ "====值:" + new String(keyValue.getValue()));  
				}  
			}  
		} catch (IOException e) {  
			e.printStackTrace();  
		}  
	}  

	/** 
	 * 查询返回表数据
	 * @param tableName 
	 */  
	@SuppressWarnings("deprecation")
	public  List<Read> QueryHbaseTable(String tableName) {
		List<Read> list = new ArrayList<Read>();
		System.out.println("正在连接habse数据库.....");  
		HTablePool pool = new HTablePool(configuration, 1000);    
		HTableInterface table = (HTableInterface) pool.getTable(tableName);
		try {  
			ResultScanner rs = table.getScanner(new Scan());
			System.out.println("连接成功，获取数据.....");  
			for (Result r : rs) {
				System.out.println("获得到rowkey:" + new String(r.getRow()));  
				Read read = new Read();
				Map<String, String> map = new HashMap<String, String>();

				read.setId(new String(r.getRow())); //获得到rowkey
				for (KeyValue keyValue : r.raw()) {
					System.out.println("family :" + new String(keyValue.getFamily())+"   key :  " + keyValue.getKeyString()+ "       value :   " + new String(keyValue.getValue()));  
					//Map<String, Object> stringMap = keyValue.toStringMap();
					//stringMap.get("qualifier").toString()
					map.put(new String(keyValue.getQualifier()), new String(keyValue.getValue()));   //列和值以key和vaule的形式存进map集合
				}  
				read.setMap(map);
				list.add(read);
			}
			rs.close();
		} catch (IOException e) {  
			e.printStackTrace();  
		}
		return list;  
	}  
	
	@RequestMapping(value = "/getData/{tableName}", method = RequestMethod.GET)
	@ResponseBody
	public List<Read> getDataByTable(@PathVariable("tableName") String tableName){
		List<Read> dataList = QueryHbaseTable(tableName);
		return dataList;
	}
} 
